package com.elaine.factory.alapi;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.elaine.constant.AlapiConstant;
import com.elaine.model.alapi.AlapiResponse;
import com.elaine.model.alapi.ShiciData;
import com.elaine.utils.HttpUtils;
import org.springframework.stereotype.Component;

/**
 * @author wrb
 * @date 2020/4/1 11:51
 */
@Component
public class ShiciAlapiFactory extends AlapiFactory {

	private static final String URL = "https://v1.alapi.cn/api/shici";

	@Override
	protected void registerFactory() {
		doRegister(AlapiConstant.SHICI, this);
	}

	@Override
	public String getReply() {
		String jsonStr = HttpUtils.requestGet(URL, null, null);
		AlapiResponse<ShiciData> alapiResponse = JSON.parseObject(jsonStr, new TypeReference<AlapiResponse<ShiciData>>() {});
		if (alapiResponse != null && alapiResponse.isSuccess()) {
			ShiciData data = alapiResponse.getData();
			return data.getOrigin() + "\n作者：" + data.getAuthor() + "\n\n" + data.getContent() + "\n\n类型：" + data.getCategory();
		}else {
			return "随机诗词请求失败咯，xrxrxr...";
		}
	}
}
